# 也是 {}
# 定义： key:value
# key 不变, 唯一

dict1 = {}
dict1[0] = True; dict1['one'] = 1; dict1[1024] = 'dicttttt'
print(dict1)
# {0: True, 'one': 1, 1024: 'dicttttt'}
print(dict1.keys())
# dict_keys([0, 'one', 1024])
print(dict1.values())
# dict_values([True, 1, 'dicttttt'])
print(dict1['one'])
# 1

# 创建2
dict2 = { 1:'1', 2:'2', '3':3 }
print('dict2', '=', end=''), print(dict2)
print(type(dict2))
# dict2 ={1: '1', 2: '2', '3': 3}
# <class 'dict'>

# 创建方法3：构造
create2 = dict([ ('Google', 1024), (666, 'spaceX') ])
print(create2)
# {'Google': 1024, 666: 'spaceX'}

# 1 作为key不得行： 
#     create4 = dict( 1='one', two=2 )
#                     ^^
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# create4 = dict( 1='one', two=2 )
create4 = dict( one='one', two=2 )
print(create4)
# {'one': 'one', 'two': 2}

# 创建方法3：推导
create3 = { x: x**2 for x in (1, 2, 3) }
print(create3)
# {1: 1, 2: 4, 3: 9}

# 字典(散列实现)， 直接遍历时，项为key
map = {aKey: aKey ** 2 for aKey in (1,2 ,3)}
for k in map:
    print(k)
# 1
# 2
# 3

# 字典 完整遍历 k:v
for k,v in map.items():
    print(k, ':', v)

# dict() 创建字典，参数不一定为 元组的列表 [(k,v),(k,v),...]
# 可以是 元组的集合
print(type( 
    dict(
        { ('a', 1), ('b', 2) }
    )
 ))
# <class 'dict'>

# 可以是 列表的列表
print(type(dict(
    [
        ['a', 1],
        ['b', 2]
    ]
)))
# <class 'dict'>

# 可以的 元组的元组
print(type(dict(
    (
        ('a', 1),
        ('b', 2)
    )
)))
# <class 'dict'>